<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>测试所有DBC兼容性改进</title>
    <style>
        body { font-family: Arial, sans-serif; margin: 20px; }
        .header { background: #f0f8ff; padding: 20px; border-radius: 10px; margin-bottom: 20px; }
        .test-section { margin: 20px 0; padding: 15px; border: 1px solid #ddd; border-radius: 5px; }
        .progress { background: #f0f0f0; border-radius: 10px; overflow: hidden; margin: 10px 0; height: 30px; }
        .progress-bar { background: #4CAF50; height: 100%; transition: width 0.3s; text-align: center; line-height: 30px; color: white; font-weight: bold; }
        .results { margin: 20px 0; }
        .file-result { margin: 5px 0; padding: 8px; border-radius: 5px; display: flex; justify-content: space-between; align-items: center; font-size: 14px; }
        .success { background: #d4edda; border-left: 4px solid #28a745; }
        .failure { background: #f8d7da; border-left: 4px solid #dc3545; }
        .warning { background: #fff3cd; border-left: 4px solid #ffc107; }
        .testing { background: #e2e3e5; border-left: 4px solid #6c757d; }
        .summary { background: #e7f3ff; padding: 15px; border-radius: 5px; margin: 20px 0; }
        .error-detail { font-size: 0.9em; color: #666; margin-top: 5px; }
        .controls { margin: 20px 0; }
        button { padding: 10px 20px; margin: 5px; border: none; border-radius: 5px; cursor: pointer; background: #007bff; color: white; }
        button:hover { background: #0056b3; }
        button:disabled { background: #6c757d; cursor: not-allowed; }
    </style>
</head>
<body>
    <div class="header">
        <h1>🧪 DBC 解析器兼容性改进测试</h1>
        <p>测试针对已知问题的改进效果，包括多路复用信号、数字开头标识符、VAL_TABLE_语法等</p>
    </div>

    <div class="controls">
        <button onclick="testKnownProblems()">测试已知问题文件</button>
        <button onclick="testAllFiles()" id="testAllBtn">完整兼容性测试</button>
        <button onclick="clearResults()">清除结果</button>
    </div>

    <div class="progress" id="progressContainer" style="display: none;">
        <div class="progress-bar" id="progressBar">0%</div>
    </div>

    <div class="summary" id="summary" style="display: none;">
        <h3>📊 测试总结</h3>
        <div id="summaryContent"></div>
    </div>

    <div class="results" id="results"></div>

    <script src="dbc-parser.js"></script>
    <script>
        // 已知问题文件列表
        const knownProblemFiles = [
            { name: 'tesla_model3_vehicle.dbc', issue: '多路复用信号' },
            { name: 'mazda_2017.dbc', issue: '数字开头标识符' },
            { name: 'cadillac_ct6_object.dbc', issue: 'VAL_TABLE_语法' },
            { name: 'ford_lincoln_base_pt.dbc', issue: '大量VAL_TABLE_' },
            { name: 'acura_ilx_2016_nidec.dbc', issue: '基准测试' }
        ];

        // 所有文件列表
        const allDbcFiles = [
            'acura_ilx_2016_nidec.dbc', 'bmw_e9x_e8x.dbc', 'cadillac_ct6_chassis.dbc',
            'cadillac_ct6_object.dbc', 'cadillac_ct6_powertrain.dbc', 'chrysler_cusw.dbc',
            'chrysler_pacifica_2017_hybrid_private_fusion.dbc', 'comma_body.dbc', 'ESR.dbc',
            'fca_giorgio.dbc', 'FORD_CADS.dbc', 'FORD_CADS_64.dbc', 'ford_cgea1_2_bodycan_2011.dbc',
            'ford_cgea1_2_ptcan_2011.dbc', 'ford_fusion_2018_adas.dbc', 'ford_fusion_2018_pt.dbc',
            'ford_lincoln_base_pt.dbc', 'gm_global_a_chassis.dbc', 'gm_global_a_high_voltage_management.dbc',
            'gm_global_a_lowspeed.dbc', 'gm_global_a_lowspeed_1818125.dbc', 'gm_global_a_object.dbc',
            'gm_global_a_powertrain_expansion.dbc', 'gwm_haval_h6_phev_2024.dbc', 'hongqi_hs5.dbc',
            'hyundai_2015_ccan.dbc', 'hyundai_2015_mcan.dbc', 'hyundai_i30_2014.dbc',
            'hyundai_kia_generic.dbc', 'hyundai_santafe_2007.dbc', 'luxgen_s5_2015.dbc',
            'mazda_2017.dbc', 'mazda_3_2019.dbc', 'mazda_radar.dbc', 'mazda_rx8.dbc',
            'mercedes_benz_e350_2010.dbc', 'nissan_xterra_2011.dbc', 'opel_omega_2001.dbc',
            'rivian_park_assist_can.dbc', 'rivian_primary_actuator.dbc', 'tesla_can.dbc',
            'tesla_model3_party.dbc', 'tesla_model3_vehicle.dbc', 'tesla_powertrain.dbc',
            'toyota_2017_ref_pt.dbc', 'toyota_adas.dbc', 'toyota_iQ_2009_can.dbc',
            'toyota_prius_2010_pt.dbc', 'toyota_radar_dsu_tssp.dbc', 'toyota_tss2_adas.dbc',
            'volvo_v40_2017_pt.dbc', 'volvo_v60_2015_pt.dbc', 'vw_meb.dbc', 'vw_mqb.dbc',
            'vw_mqbevo.dbc', 'vw_pq.dbc'
        ];

        let currentTest = null;

        function showProgress() {
            document.getElementById('progressContainer').style.display = 'block';
        }

        function hideProgress() {
            document.getElementById('progressContainer').style.display = 'none';
        }

        function updateProgress(current, total) {
            const percent = Math.round((current / total) * 100);
            const progressBar = document.getElementById('progressBar');
            progressBar.style.width = percent + '%';
            progressBar.textContent = `${current}/${total} (${percent}%)`;
        }

        function addResult(filename, status, details, issue = null) {
            const resultsDiv = document.getElementById('results');
            const resultDiv = document.createElement('div');
            resultDiv.className = `file-result ${status}`;
            
            let statusIcon = status === 'success' ? '✅' : status === 'failure' ? '❌' : status === 'warning' ? '⚠️' : '⏳';
            let mainText = `${statusIcon} ${filename}`;
            if (issue) mainText += ` (${issue})`;
            
            resultDiv.innerHTML = `
                <div>
                    <strong>${mainText}</strong>
                    <div class="error-detail">${details}</div>
                </div>
            `;
            
            resultsDiv.appendChild(resultDiv);
        }

        function clearResults() {
            document.getElementById('results').innerHTML = '';
            document.getElementById('summary').style.display = 'none';
            hideProgress();
        }

        async function testFile(filename) {
            return new Promise((resolve) => {
                const dbcPath = `./${filename}`;
                
                fetch(dbcPath)
                    .then(response => {
                        if (!response.ok) {
                            throw new Error(`文件不存在或无法访问: ${response.status}`);
                        }
                        return response.text();
                    })
                    .then(content => {
                        const startTime = Date.now();
                        
                        try {
                            const database = parseDBCContent(content, filename);
                            const endTime = Date.now();
                            const parseTime = endTime - startTime;
                            
                            const signals = database.frames.reduce((sum, frame) => sum + frame.signals.length, 0);
                            
                            let status = 'success';
                            if (database.frames.length === 0) {
                                status = 'warning';
                            }
                            
                            const details = `${parseTime}ms, ${database.frames.length}帧, ${signals}信号, ${database.warnings.length}警告`;
                            
                            resolve({
                                filename,
                                status,
                                details,
                                frames: database.frames.length,
                                signals,
                                warnings: database.warnings.length,
                                parseTime
                            });
                        } catch (error) {
                            resolve({
                                filename,
                                status: 'failure',
                                details: `解析错误: ${error.message}`,
                                error: error.message
                            });
                        }
                    })
                    .catch(error => {
                        resolve({
                            filename,
                            status: 'failure',
                            details: `文件访问错误: ${error.message}`,
                            error: error.message
                        });
                    });
            });
        }

        async function testKnownProblems() {
            clearResults();
            
            document.getElementById('testAllBtn').disabled = true;
            showProgress();
            
            console.log('🧪 开始测试已知问题文件...');
            
            for (let i = 0; i < knownProblemFiles.length; i++) {
                const file = knownProblemFiles[i];
                updateProgress(i, knownProblemFiles.length);
                
                addResult(file.name, 'testing', `正在测试 ${file.issue}...`, file.issue);
                
                const result = await testFile(file.name);
                
                // 更新结果
                const lastResult = document.querySelector('.file-result:last-child');
                lastResult.className = `file-result ${result.status}`;
                
                let statusIcon = result.status === 'success' ? '✅' : result.status === 'failure' ? '❌' : '⚠️';
                lastResult.innerHTML = `
                    <div>
                        <strong>${statusIcon} ${file.name} (${file.issue})</strong>
                        <div class="error-detail">${result.details}</div>
                    </div>
                `;
                
                // 短暂延迟以显示进度
                await new Promise(resolve => setTimeout(resolve, 100));
            }
            
            updateProgress(knownProblemFiles.length, knownProblemFiles.length);
            setTimeout(() => {
                hideProgress();
                document.getElementById('testAllBtn').disabled = false;
            }, 500);
        }

        async function testAllFiles() {
            clearResults();
            
            document.getElementById('testAllBtn').disabled = true;
            showProgress();
            
            const results = [];
            let successCount = 0;
            let failureCount = 0;
            let zeroFrameCount = 0;
            
            console.log('🚀 开始完整兼容性测试...');
            
            for (let i = 0; i < allDbcFiles.length; i++) {
                const filename = allDbcFiles[i];
                updateProgress(i, allDbcFiles.length);
                
                const result = await testFile(filename);
                results.push(result);
                
                if (result.status === 'success') {
                    successCount++;
                    if (result.frames === 0) {
                        zeroFrameCount++;
                    }
                } else {
                    failureCount++;
                }
                
                addResult(filename, result.status, result.details);
                
                // 短暂延迟以显示进度
                await new Promise(resolve => setTimeout(resolve, 50));
            }
            
            // 显示总结
            updateProgress(allDbcFiles.length, allDbcFiles.length);
            
            const summaryDiv = document.getElementById('summary');
            const successRate = Math.round((successCount / allDbcFiles.length) * 100);
            const effectiveRate = Math.round(((successCount - zeroFrameCount) / allDbcFiles.length) * 100);
            
            summaryDiv.innerHTML = `
                <h3>📊 测试总结</h3>
                <div>
                    <p><strong>总计:</strong> ${allDbcFiles.length} 个文件</p>
                    <p><strong>成功:</strong> ${successCount} 个文件 (${successRate}%)</p>
                    <p><strong>失败:</strong> ${failureCount} 个文件</p>
                    <p><strong>成功但0帧:</strong> ${zeroFrameCount} 个文件</p>
                    <p><strong>有效解析率:</strong> ${effectiveRate}% (${successCount - zeroFrameCount}/${allDbcFiles.length})</p>
                </div>
            `;
            summaryDiv.style.display = 'block';
            
            setTimeout(() => {
                hideProgress();
                document.getElementById('testAllBtn').disabled = false;
            }, 500);
            
            console.log('✅ 完整兼容性测试完成');
        }

        // 页面加载完成后的初始化
        document.addEventListener('DOMContentLoaded', function() {
            console.log('🔧 DBC 解析器兼容性测试页面已加载');
            console.log('💡 使用"测试已知问题文件"来验证特定修复');
            console.log('🚀 使用"完整兼容性测试"来测试所有文件');
        });
    </script>
</body>
</html> 